package com.gccloud.design;

import com.gccloud.design.dto.DesignCenterDTO;
import com.gccloud.design.dto.SdFormActionConfDto;
import com.gccloud.design.dto.SdFormConfigDto;
import com.gccloud.design.dto.SdFormDefineDto;
import com.gccloud.design.entity.SdFormActionConfEntity;
import com.gccloud.design.entity.SdFormDefineEntity;
import com.gccloud.design.service.SdFormConfigService;
import com.gccloud.design.service.SdFormDefineService;
import com.gccloud.design.vo.SdFormDefineVo;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wu.yumin
 * @version 1.0.0
 * @Description 表单定义
 * @date 2021/6/16 18:00
 */
@RestController
@RequestMapping("/form")
@Api(tags ="表单定义API")
public class SdFormDefineController {
    @Resource
    private SdFormDefineService sdFormDefineService;
    @Resource
    private SdFormConfigService sdFormConfigService;

    @ApiOperation(value = "分页查询表单列表",produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<SdFormDefineVo>> queryFormList(SdFormDefineDto searchDTO) {
        Map<String,Object> pageList = sdFormDefineService.queryFormList(searchDTO);
        PageVO<SdFormDefineVo> pageVo = new PageVO<>();
        pageVo.setList((List<SdFormDefineVo>) pageList.get("list"));
        pageVo.setTotalCount((Long) (pageList.get("totalCount")));
        return R.success(pageVo);
    }

    @ApiOperation(value = "查询表单列表",produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/formList")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "busiType", value = "业务类型", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "formClass", value = "表单分类", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "formType", value = "表单类型", paramType = "query", dataType = "string")
    })
    public R<List<SdFormDefineVo>> queryList(@RequestParam Long busiType,@RequestParam String formClass,@RequestParam String formType) {
       List<SdFormDefineEntity> list = sdFormDefineService.queryList(busiType,formClass,formType);
        List<SdFormDefineVo> voList = Lists.transform(list, (entity) -> {
            SdFormDefineVo convert = BeanConvertUtils.convert(entity, SdFormDefineVo.class);
            return convert;
        });
        return R.success(voList);
    }

    @PostMapping("/add")
    @ApiOperation("新增表单")
    public R<Object> save(@RequestBody SdFormDefineVo vo){
        SdFormDefineEntity entity = BeanConvertUtils.convert(vo, SdFormDefineEntity.class);
        return sdFormDefineService.insertFormDefine(entity);
    }

    @PostMapping("/addList")
    @ApiOperation("新增多个表单")
    public R saveList(@RequestBody Map<String,Object> param){
        return sdFormDefineService.saveList(param);
    }


    @PostMapping("/update")
    @ApiOperation("更新表单")
    public R<Object> update(@RequestBody SdFormDefineVo vo){
        SdFormDefineEntity entity = BeanConvertUtils.convert(vo, SdFormDefineEntity.class);
        sdFormDefineService.updateFormDefine(entity);
        return R.success();
    }

    @GetMapping("/get/{formCode}")
    @ApiOperation("根据表单编码查询表单信息")
    @ApiImplicitParam(name = "formCode", value = "表单编码", required = true, dataType = "String", paramType = "path")
    public R<SdFormDefineVo> getFormByCd(@PathVariable("formCode") String formCode){
        SdFormDefineEntity form= sdFormDefineService.getFormByCd(formCode);
        if(form == null){
            return R.success();
        }
        SdFormDefineVo convert = BeanConvertUtils.convert(form, SdFormDefineVo.class);
        return R.success(convert);
    }

    @PostMapping("/del/{ids}")
    @ApiOperation("删除表单")
    @ApiImplicitParam(name = "ids", value = "表单Id数组", required = true,allowableValues = "true", dataType = "Long", paramType = "path")
    public R<Object> delete(@PathVariable("ids") Long[] ids){
        sdFormDefineService.delFormDefine(ids);
        return R.success();
    }


    @PostMapping("/downForm/{catalogCode}")
    @ApiOperation("低代码平台下线表单")
    @ApiImplicitParam(name = "catalogCode", value = "场景编码", required = true, dataType = "String", paramType = "path")
    public R downForm(@PathVariable("catalogCode") String catalogCode){
        boolean result=sdFormDefineService.downForm(catalogCode);
        if(result){
            return R.success();
        }else {
            return R.error();
        }
    }

    @PostMapping("/release/{ids}")
    @ApiOperation("表单发布")
    @ApiImplicitParam(name = "ids", value = "表单Id数组", required = true, dataType = "Long", paramType = "path")
    public R<Object> release(@PathVariable("ids") Long[] ids){
        sdFormDefineService.releaseForm(ids);
        return R.success();
    }

    @ApiOperation(value = "查询表单操作组合配置",produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/formAction/{formId}")
    @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Long", paramType = "path")
    public R<List<SdFormActionConfDto>> queryFormList(@PathVariable("formId") Long formId) {
        List<SdFormActionConfDto> list = sdFormConfigService.queryActionsByFormId(formId);

        return R.success(list);
    }

    @ApiOperation(value = "新增表单操作组合配置",produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/formAction/add")
    public R<Object> addFormAction(@RequestBody List<SdFormActionConfEntity> list) {
        sdFormConfigService.saveFormAction(list);
        return R.success();
    }

    @PostMapping("/formAction/del/{formId}/{actionId}")
    @ApiOperation("移除表单操作配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "actionId", value = "操作Id", required = true, dataType = "Long", paramType = "path")
    })
    public R<Object> delete(@PathVariable("formId") Long formId,@PathVariable("actionId") Long actionId){
        sdFormConfigService.delFormAction(formId,actionId);
        return R.success();
    }

    @PostMapping("/formAction/update")
    @ApiOperation("更新表单操作配置(排序)")
    public R<Object> updateFormAction(@RequestBody List<SdFormActionConfEntity> list){
       sdFormConfigService.updateFormAction(list);
       return R.success();
    }

    @PostMapping("/formRepo/bind")
    @ApiOperation("表单生成环节")
    public R<Object> formRepoBind(@RequestBody SdFormConfigDto dto){
        sdFormConfigService.formRepoBind(dto);
        return R.success();
    }

    @PostMapping("/formRepo/update")
    @ApiOperation("表单生成环节修改")
    public R<Object> updateFormRepoBind(@RequestBody SdFormConfigDto dto){
        sdFormConfigService.updateFormRepoBind(dto);
        return R.success();
    }

    @PostMapping("/formRepo/delete/{repoId}")
    @ApiImplicitParam(name = "repoId", value = "环节Id", required = true, dataType = "Long", paramType = "path")
    @ApiOperation("删除环节")
    public R<Object> deleteRepoBind(@PathVariable(value = "repoId")String repoId){
        sdFormConfigService.deleteRepoBind(repoId);
        return R.success();
    }

    @ApiOperation(value = "查询表单环节绑定信息",produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/formRepo/{formId}")
    @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Long", paramType = "path")
    public R<SdFormConfigDto> queryFormRepoInfo(@PathVariable("formId") Long formId) {
        SdFormConfigDto dto = sdFormConfigService.queryFormRepoInfo(formId);
        return R.success(dto);
    }
}
